package com.jason.www.dao;

import com.jason.www.domain.Orders;
import com.jason.www.domain.vo.OrderVo;
import com.jason.www.domain.vo.OrdersUserVo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

public interface OrdersMapper {

    @Delete({
        "delete from orders",
        "where id = #{id,jdbcType=BIGINT}"
    })
    int deleteByPrimaryKey(Long id);

    @Insert({
        "insert into orders (id, user_id, ",
        "number, createtime, ",
        "note)",
        "values (#{id,jdbcType=BIGINT}, #{userId,jdbcType=BIGINT}, ",
        "#{number,jdbcType=VARCHAR}, #{createtime,jdbcType=DATE}, ",
        "#{note,jdbcType=LONGVARCHAR})"
    })
    int insert(Orders record);

    @InsertProvider(type=OrdersSqlProvider.class, method="insertSelective")
    int insertSelective(Orders record);

    @Select({
        "select",
        "id, user_id, number, createtime, note",
        "from orders",
        "where id = #{id,jdbcType=BIGINT}"
    })
    @Results(id = "ordersMap",
            value = {   @Result(column="id", property="id", jdbcType=JdbcType.BIGINT, id=true),
                    @Result(column="user_id", property="userId", jdbcType=JdbcType.BIGINT),
                    @Result(column="number", property="number", jdbcType=JdbcType.VARCHAR),
                    @Result(column="createtime", property="createtime", jdbcType=JdbcType.DATE),
                    @Result(column="note", property="note", jdbcType=JdbcType.LONGVARCHAR)
    })
    Orders selectByPrimaryKey(Long id);

    @UpdateProvider(type=OrdersSqlProvider.class, method="updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(Orders record);

    @Update({
        "update orders",
        "set user_id = #{userId,jdbcType=BIGINT},",
          "number = #{number,jdbcType=VARCHAR},",
          "createtime = #{createtime,jdbcType=DATE},",
          "note = #{note,jdbcType=LONGVARCHAR}",
        "where id = #{id,jdbcType=BIGINT}"
    })
    int updateByPrimaryKeyWithBLOBs(Orders record);

    @Update({
        "update orders",
        "set user_id = #{userId,jdbcType=BIGINT},",
          "number = #{number,jdbcType=VARCHAR},",
          "createtime = #{createtime,jdbcType=DATE}",
        "where id = #{id,jdbcType=BIGINT}"
    })
    int updateByPrimaryKey(Orders record);

    @Select({
            "SELECT",
            "u.`user_name` userName,",
            "u.`user_age`  userAge,",
            "u.`user_sex`  userSex,",
            "o.`user_id`  userId,",
            "o.*",
            "FROM orders o ,USER u",
            "WHERE o.`user_id`=u.`id`"
    })
    List<OrdersUserVo> getOrdersUser();

    @Select({
            "SELECT * FROM orders WHERE orders.`number`=#{orderNum}"
    })
    @Results({
            @Result(column="id", property="id", jdbcType=JdbcType.BIGINT, id=true),
            @Result(column="user_id", property="userId", jdbcType=JdbcType.BIGINT),
            @Result(column="number", property="number", jdbcType=JdbcType.VARCHAR),
            @Result(column="createtime", property="createtime", jdbcType=JdbcType.DATE),
            @Result(column="note", property="note", jdbcType=JdbcType.LONGVARCHAR),
            @Result(column="id",property="orderDetailss",
                    many=@Many(
                            select="com.jason.www.dao.OrderDetailMapper.selectByOrdersId",
                            fetchType=FetchType.LAZY))
    })
    List<OrderVo> getOrderAndOrderDatil(String orderNum);
}